<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload and Similarity Search</title>
    <style>
        /* 将CSS代码直接嵌入到<style>标签中 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .container {
            text-align: center;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            width: 100%;
        }
        h1 {
            margin-bottom: 20px;
        }
        #uploadForm {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        #imageUpload {
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: #007BFF;
            color: #fff;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .similar-images {
            margin-top: 30px;
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 10px;
        }
        .similar-images img {
            max-width: 100px;
            max-height: 100px;
            object-fit: cover;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            cursor: pointer;
            transition: transform 0.2s;
        }
        .similar-images img:hover {
            transform: scale(1.05);
        }
    </style>
</head>
<body>
<div class="container">
    <h1>Upload an Image to Find Similar Ones</h1>
    <form id="uploadForm">
        <input type="file" id="imageUpload" accept="image/*" required>
        <button type="submit">Upload</button>
    </form>
    <div id="similarImages" class="similar-images"></div>
</div>
<script>
        // 将JavaScript代码直接嵌入到<scriptsimilarImages>标签中
        document.getElementById('uploadForm').addEventListener('submit', async function(event) {
            event.preventDefault();

            const imageInput = document.getElementById('imageUpload');
            const file = imageInput.files[0];

            if (!file) {
                alert('Please select an image.');
                return;
            }

            const formData = new FormData();
            formData.append('file', file);

            try {
                const response = await fetch('http://localhost:8081/face/faceSearch', {
                    method: 'POST',
                    body: formData
                });

                const data = await response.json();
                console.log(data);


                displaySimilarImages(data.data);
            } catch (error) {
                console.error('Error uploading image:', error);
                alert('Failed to upload image. Please try again.');
            }
        });

        function displaySimilarImages(data) {
            const similarImagesContainer = document.getElementById('similarImages');
            similarImagesContainer.innerHTML = '';
            data.forEach(user => {
                const imgElement = document.createElement('img');
                imgElement.src = user.faceFeatureUrl; // Adjust based on your API response structure
                similarImagesContainer.appendChild(imgElement);
                 const imgElement2 = document.createElement('img');
                imgElement2.src = user.faceUrl;
                similarImagesContainer.appendChild(imgElement2);

            });
        }
    </script>
</body>
</html>